package com.admin.controller;

import com.admin.bean.User;
import com.admin.common.Result;
import com.admin.common.ResultUtil;
import com.admin.util.SessionUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p> </p>
 *
 * @Author: Hanson
 * @time 2019/8/10 21:00
 */
@RestController
@RequestMapping("/api/backend/main")
public class LoginController {

    private static final Logger LOGGER = LogManager.getLogger(LoginController.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @RequestMapping("/login")
    public Result login(){
        User user = new User();
        user.setName("男神");
        SessionUtil.setCurrentUser(user);
        String sql = "select username, pwd from tb_user";
        LOGGER.debug("全部User的sql:{}", sql);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return ResultUtil.success(list);
    }

    @ResponseBody
    @RequestMapping("/all")
    public Result all(){
        String sql = "select username from tb_user limit 1";
        LOGGER.debug("全部User的sql:{}", sql);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return ResultUtil.success(list);
    }
}
